/*###################################################################*
#
# MP (Mitchell Pell) Base - CrossPlatform Base Utilities.
# Copyright (C) 2014  Mitchell Pell
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
####################################################################*/
#ifndef BASE_COMMAND_CONSOLE_H
#define BASE_COMMAND_CONSOLE_H

#define CMDLISTENER_HASHMAP_SIZE 256

#include <hash_map>
#include "Console.h"
#include "Command.h" //Commands
#include "CommandListener.h" //Command Listeners/Templates
#include <exception>

using namespace std;

namespace mp_base{
	namespace console{

		//Vector of Shared Pointer Commands
		typedef vector<spCommand> vCommands;
		typedef vector<spCommand>::const_iterator ciCommand;

		//Hash Map of Shared Pointer Command Listeners
		struct CommandCompare{
			bool operator() (const string& command, const string& listener)const
				{ return command.compare(listener) == 0; } };
		typedef hash_map<string, spCommandListener> hmCommandListeners;
		typedef pair<string, spCommandListener> hmCommandListener;

		

		/*!----------------------------------------------------------------
		*	@class		CommandConsole
		*	@brief		Everything a Console is with the addition of the
		*				ability to send Commands to objects that are 
		*				registered listeners.
		*
		*	@author		Mitchell Pell
		*	@date		11/23/2013
		*	@version	1.0
		*
		*	@revisions	Revision History:
		*
		*					- Version 1.0 11/23/2013
		*						-# Initial Make
		*
		-----------------------------------------------------------------*/
		class CommandConsole : public Console
		{
		public:
			CommandConsole() : Console() {}

			bool SendCommand(const string cmd);
			bool SendCommand(const char* cmd);
			bool SendCommand(spCommand cmd);

			void RegisterCommandListener(spCommandListener cmd);
			void RemoveCommandListener(spCommandListener cmd);

			//----------------

			const ciCommand GetInputBufferEnd() const
				{ return m_inputBuffer.end();}

			const ciCommand GetInputBufferBegin() const
				{ return m_inputBuffer.begin();}

		private:

			bool ProcessCommand(spCommand& cmd);

		protected:
			vCommands			m_inputBuffer;
			hmCommandListeners	m_listeners;
	
		public:
			static const string	CMD_CONSOLE_PREFIX;
			static const string	CMD_CONSOLE_ERROR_PREFIX;
			static const string	CMD_NOT_FOUND_ERROR;
		};
	}
}

#endif